## script to generate graphics for TPG paper

# preliminaries
rm(list = ls())

# pacakges
#library(foreign)
library(ggplot2)
library(gridExtra)

# load data
data <- read.csv("TPG.csv",
                 stringsAsFactors = F,
                 header = T)

# set working directory to store output
#setwd("~/Dropbox/Research/Master's_Thesis/PLOS_One_11-19-15/Paper/images")

# figure 2 a
plot_data <- data[which(data$cT1 == 1),]
pdf(file = "Figure_2_a.pdf", height = 4, width = 6)
m <- ggplot(plot_data,
            aes(x=info_mean)) +
    geom_histogram(aes(y=(..density..)*0.3),
                   position='dodge',
                   binwidth=0.3,
                   fill = "#0072B2",
                   colour = "black")  +
    scale_x_continuous(breaks=0:10,
                       limits = c(0,10)) +
    xlab("Mean of Information") +
    ylab("Percent")
print(m)
dev.off()


# figure 2 b
plot_data <- data[which(data$cT2 == 1),]
pdf(file = "Figure_2_b.pdf", height = 4, width = 6)
m <- ggplot(plot_data,
            aes(x=info_mean)) +
    geom_histogram(aes(y=(..density..)*0.3),
                   position='dodge',
                   binwidth=0.3,
                   fill = "#0072B2",
                   colour = "black")  +
    scale_x_continuous(breaks=0:10,
                       limits = c(0,10)) +
    xlab("Mean of Information") +
    ylab("Percent")
print(m)
dev.off()

# figure 2 combined
pdf(file = "Figure_2.pdf", height = 3, width = 7.5)
plot_data <- data[which(data$cT1 == 1),]
m1 <- ggplot(plot_data,
            aes(x=info_mean)) +
    geom_histogram(aes(y=(..density..)*0.3),
                   position='dodge',
                   binwidth=0.3,
                   fill = "#0072B2",
                   colour = "black")  +
    scale_x_continuous(breaks=0:10,
                       limits = c(0,10)) +
    xlab("Mean of Information") +
    ylab("Percent")

plot_data <- data[which(data$cT2 == 1),]
m2 <- ggplot(plot_data,
            aes(x=info_mean)) +
    geom_histogram(aes(y=(..density..)*0.3),
                   position='dodge',
                   binwidth=0.3,
                   fill = "#0072B2",
                   colour = "black")  +
    scale_x_continuous(breaks=0:10,
                       limits = c(0,10)) +
    xlab("Mean of Information") +
    ylab("Percent")


grid.arrange(m1, m2, ncol=2)
dev.off()



# figure 3 a
plot_data <- data[which(data$cT1 == 1),]
pdf(file = "Figure_3_a.pdf", height = 4, width = 6)
m <- ggplot(plot_data,
            aes(x=info_sd)) +
    geom_histogram(aes(y=(..density..)*0.3),
                   position='dodge',
                   binwidth=0.3,
                   fill = "#0072B2",
                   colour = "black")  +
    scale_x_continuous(breaks=0:10,
                       limits = c(0,5)) +
    xlab("Standard Deviation of Information") +
    ylab("Percent")
print(m)
dev.off()


# figure 3 b
plot_data <- data[which(data$cT2 == 1),]
pdf(file = "Figure_3_b.pdf", height = 4, width = 6)
m <- ggplot(plot_data,
            aes(x=info_sd)) +
    geom_histogram(aes(y=(..density..)*0.3),
                   position='dodge',
                   binwidth=0.3,
                   fill = "#0072B2",
                   colour = "black")  +
    scale_x_continuous(breaks=0:10,
                       limits = c(0,5)) +
    xlab("Standard Deviation of Information") +
    ylab("Percent")
print(m)
dev.off()

# plot figure 3 combined
pdf(file = "Figure_3.pdf", height = 3, width = 7.5)
plot_data <- data[which(data$cT1 == 1),]
m1 <- ggplot(plot_data,
            aes(x=info_sd)) +
    geom_histogram(aes(y=(..density..)*0.3),
                   position='dodge',
                   binwidth=0.3,
                   fill = "#0072B2",
                   colour = "black")  +
    scale_x_continuous(breaks=0:10,
                       limits = c(0,5)) +
    xlab("Standard Deviation of Information") +
    ylab("Percent")

plot_data <- data[which(data$cT2 == 1),]
m2 <- ggplot(plot_data,
            aes(x=info_sd)) +
    geom_histogram(aes(y=(..density..)*0.3),
                   position='dodge',
                   binwidth=0.3,
                   fill = "#0072B2",
                   colour = "black")  +
    scale_x_continuous(breaks=0:10,
                       limits = c(0,5)) +
    xlab("Standard Deviation of Information") +
    ylab("Percent")

grid.arrange(m1, m2, ncol=2)
dev.off()


# The I treatment is cT1=1 using allocation1 and guess1 (mean of allocations is 5.86 s.d. 2.13)  OR using allocation2 guess2
#
# The NI treatment is cT1=0 & cT2=0 using allocation1 and guess 1 (mn 5.99 (2.51))
#
# The H-NI treatment is cT1=0, cT2=1 using allocation1 and guess1 mn 6.11 (2.15))

# The H-I treatment is cT2=1 using allocation2 and guess2 mn 5.97 (2.20)

I = data[which(data$cT2 == 1),]

I = data.frame(allocation = c(I$allocation1,I$allocation2),
                guess = c(I$guess1,I$guess2),
                type = rep("I",2*nrow(I)),
                stringsAsFactors = F)

NI = data[which(data$cT2 == 0),]
NI = NI[which(NI$cT1 == 0),]

NI = data.frame(allocation = NI$allocation1,
                guess = NI$guess1,
                type = rep("NI",nrow(NI)),
                stringsAsFactors = F)

HNI = data[which(data$cT1 == 0),]
HNI = HNI[which(HNI$cT2 == 1),]

HNI = data.frame(allocation = HNI$allocation1,
                guess = HNI$guess1,
                type = rep("H-NI",nrow(HNI)),
                stringsAsFactors = F)


HI = data[which(data$cT2 == 1),]

HI = data.frame(allocation = HI$allocation2,
                 guess = HI$guess2,
                 type = rep("H-I",nrow(HI)),
                 stringsAsFactors = F)

# figure 4
figure_4 <- rbind(I,NI,HNI,HI)

colnames(figure_4) <- c("Allocation", "Belief","Treatment")

m <- ggplot(figure_4,
            aes(x=Allocation, fill = type)) +
    geom_histogram(aes(y = (..count..)/sum(..count..)),
                   position='dodge',
                   colour = "black") +
    scale_x_discrete(breaks=0:10,
                      limits = c(-1,11)) +
    xlab("Standard Deviation of Information") +
    ylab("Percent")
print(m)


library(dplyr)
d2 <- figure_4 %>%
    group_by(Treatment,Allocation) %>%
    summarise(count=n()) %>%
    mutate(perc=count/sum(count))
pdf(file = "Figure_5.pdf", height = 4, width = 7.5)
ggplot(d2, aes(Allocation, y=perc*100, fill=Treatment)) +
    geom_bar(stat="identity",
             position='dodge',
             colour = "black")+
    scale_fill_brewer() +
    scale_x_discrete("Allocation",labels = c("0"="0","1"="1","2"="2","3"="3","4"="4","5"="5","6" = "6", "7"="7","8"="8","9"="9","10"="10"),
                     limits = c(0,1,2,3,4,5,6,7,8,9,10),
                     expand = c(0.05,0)) +
    xlab("Allocation") +
    ylab("Percent")
dev.off()

d2 <- figure_4 %>%
    group_by(Treatment,Belief) %>%
    summarise(count=n()) %>%
    mutate(perc=count/sum(count))
pdf(file = "Figure_4.pdf", height = 4, width = 7.5)
ggplot(d2, aes(Belief, y=perc*100, fill=Treatment)) +
    geom_bar(stat="identity",
             position='dodge',
             colour = "black")+
    scale_fill_brewer()+
    xlab("Beliefs") +
    ylab("Percent") +
    scale_x_discrete("Belief",labels = c("0"="0","1"="1","2"="2","3"="3","4"="4","5"="5","6" = "6", "7"="7","8"="8","9"="9","10"="10"),
                     limits = c(0,1,2,3,4,5,6,7,8,9,10),
                     expand = c(0.05,0))

dev.off()
